package com.cms.intelligent_control.module

import android.app.Activity
import android.media.MediaPlayer
import android.os.Environment
import android.view.View
import android.widget.MediaController
import android.widget.VideoView
import com.cms.intelligent_control.R
import com.cms.intelligent_control.common.event.PlayCompletionEvent
import com.cms.intelligent_control.utils.isAudioFile
import com.cms.intelligent_control.utils.isUrl
import com.cms.intelligent_control.utils.isVideoFile
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject
import java.io.File


class VideoModule(var activity: Activity) {
    private val mVideoView = activity.findViewById<VideoView>(R.id.video_view)

    private var playType = ""
    private var playStatus = Status.STATUS_READY //1、就绪 2、播放 3、暂停
    private val mMediaPlayer = MediaPlayer()

    public fun isAudioVideoAction(data: JSONObject): Boolean {
        val type = data.optString("instruct_type")
        return arrayOf("音视频播放").contains(type)
    }

    public fun execute(data: JSONObject) {
        val type = data.optString("instruct_type")
        val name = data.optString("instruct_name")
        val operation = data.optString("exe_operation")
        if (type == "音视频播放") {
            if (isAudioFile(operation)) {
                playAudio(operation)
            } else if (isVideoFile(operation)) {
                playVideo(operation)
            }
        }
    }

    private fun playAudio(filename: String) {
        try {
            var dataSource = filename
            if (!isUrl(dataSource)) {
                val file = File(Environment.getExternalStorageDirectory(), filename)
                if (!file.exists()) {
                    return
                }
                dataSource = file.path
            }
            mMediaPlayer.setDataSource(dataSource)
            mMediaPlayer.setOnCompletionListener {
                playStatus = Status.STATUS_READY
                EventBus.getDefault().post(PlayCompletionEvent("audio"))
            }
            mMediaPlayer.prepare()
            mMediaPlayer.start()
            playStatus = Status.STATUS_PLAY
            playType = "audio"
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun playVideo(filename: String) {
        try {
            var dataSource = filename
            if (!isUrl(dataSource)) {
                val file = File(Environment.getExternalStorageDirectory(), filename)
                if (!file.exists()) {
                    return
                }
                dataSource = file.path
            }
            dataSource = "http://gcalic.v.myalicdn.com/gc/jyg04_1/index.m3u8"
            mVideoView.visibility = View.VISIBLE
            mVideoView.setMediaController(MediaController(activity))
            mVideoView.setOnCompletionListener {
                mVideoView.visibility = View.GONE
                playStatus = Status.STATUS_READY
                EventBus.getDefault().post(PlayCompletionEvent("video"))
            }
            mVideoView.setVideoPath(dataSource)
            mVideoView.start()
            playStatus = Status.STATUS_PLAY
            playType = "video"
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    public fun intercept(data: JSONObject): Boolean {
        val type = data.optString("instruct_type")
        val name = data.optString("instruct_name")
        var intercept = false
        if (playStatus == Status.STATUS_PLAY) {
            intercept = true
        }
        if (playStatus == Status.STATUS_PAUSE) {
            if (type == "录音控制" && name == "录音开始") {
                intercept = true
            }
            if (type.isNotBlank() && type != "录音控制") {
                stop()
            }
        }
        if (type == "智能打断") { //关闭音视频
            stop()
        } else if (type == "录音控制") {
            if (name == "录音暂停" && playStatus == Status.STATUS_PLAY) {
                pause()
            }
            if (name == "录音开始" && playStatus == Status.STATUS_PAUSE) {
                play()
            }
        }
        return intercept
    }

    public fun play() {
        if (playStatus == Status.STATUS_PAUSE) {
            if (playType == "audio") {
                mMediaPlayer.start()
            } else if (playType == "video") {
                mVideoView.start()
            }
        }
    }

    public fun pause() {
        if (playStatus == Status.STATUS_PLAY) {
            if (playType == "audio") {
                mMediaPlayer.pause()
            } else if (playType == "video") {
                mVideoView.pause()
            }
            playStatus = Status.STATUS_PAUSE
        }
    }

    public fun stop() {
        mMediaPlayer.stop()
        mVideoView.stopPlayback()
        mVideoView.visibility = View.GONE
        playStatus = Status.STATUS_READY
        EventBus.getDefault().post(PlayCompletionEvent(playType))
    }


    public fun destroy() {
        mMediaPlayer.stop()
        mMediaPlayer.release()
    }

    public fun getPlayStatus(): Status {
        return playStatus
    }

    enum class Status {
        STATUS_READY, //就绪
        STATUS_PLAY,  //播放
        STATUS_PAUSE  //暂停
    }

}